package microcredit.gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.security.NoSuchAlgorithmException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.xml.bind.ValidationException;

import microcredit.model.bo.BOAuthentication;
import net.miginfocom.swing.MigLayout;

/**
 * Class represents the Authentication Dialog. Just use
 * {@link #setVisible(boolean)} to show the dialog.
 * 
 * @author Christian Humer
 */
public class LoginDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	/** The keyword label on the left side of the password field. */
	private JLabel keywordLabel;
	/** The password field, where the user enters the password. */
	private JPasswordField passwordField;
	/** The button the user needs to press to login */
	private JButton loginButton;

	/**
	 * Constructs a new Login Dialog instance.
	 */
	public LoginDialog() {
		super(JOptionPane.getRootFrame(), true);
		initialize();
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		pack();
		setLocationRelativeTo(getParent());
	}

	/**
	 * Performs the login action using the BOAuthentication class and the
	 * contents of the passwordField.
	 */
	private void login() {
		BOAuthentication authentication = new BOAuthentication();
		try {
			boolean success = authentication.login(getPasswordField()
					.getPassword());
			if (success) {
				dispose();
			}
		} catch (ValidationException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		} catch (NoSuchAlgorithmException e) {
		}
	}

	/**
	 * Initializes the ui and layouts the components on screen using MigLayout.
	 */
	private void initialize() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowActivated(WindowEvent e) {
				getPasswordField().requestFocus();
			}
		});
		setTitle("Login");

		// layout the components
		getContentPane().setLayout(new MigLayout("", "[][][grow][]", "[][][]"));
		getContentPane().add(getKeywordLabel(), "cell 1 1,alignx trailing");
		getContentPane().add(getPasswordField(), "cell 2 1,growx");
		getContentPane().add(getLoginButton(), "cell 2 2");
	}

	private JLabel getKeywordLabel() {
		if (keywordLabel == null) {
			keywordLabel = new JLabel("Keyword");
			keywordLabel.setFont(new Font("SansSerif", Font.BOLD, 16));
			keywordLabel.setName("keywordLabel");
			keywordLabel.setName("label");
		}
		return keywordLabel;
	}

	private JPasswordField getPasswordField() {
		if (passwordField == null) {
			passwordField = new JPasswordField();
			passwordField.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					login();
				}
			});
			passwordField.setColumns(10);
			passwordField.setFont(new Font("SansSerif", Font.BOLD, 16));
			passwordField.setName("passwordField");
		}
		return passwordField;
	}

	private JButton getLoginButton() {
		if (loginButton == null) {
			loginButton = new JButton("Login");
			loginButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					login();
				}
			});
			loginButton.setFont(new Font("SansSerif", Font.BOLD, 16));
			loginButton.setName("loginButton");
			loginButton.setName("button");
		}
		return loginButton;
	}
}
